package com.zzptc.myk.a114;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zzptc.myk.a114.entity.Student;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    ListView listView;
    List<Student>list_myk;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化控件
        listView=findViewById(R.id.lv_myk);
//使用循环添加数据
        list_myk=new ArrayList<>();
        for (int i=1;i<=100;i++){
            Student student = new Student(R.mipmap.a, i, "姓名" + i, new Date() + "");
            list_myk.add(student);

        }
        //打印日志
        for(Student s:list_myk){
            Log.d(TAG, s.getMy_name());
        }

        //适配器
        MyAdapter myAdapter=new MyAdapter();
        listView.setAdapter(myAdapter);

    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list_myk.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //要数据
            Student student=list_myk.get(position);
            //创建小界面
            View inflate = View.inflate(MainActivity.this, R.layout.item, null);
//绑定数据，之前要实例化控件
            ImageView iv_pic = inflate.findViewById(R.id.iv_pic);
            iv_pic.setImageResource(R.mipmap.a);//调取照片

            TextView tv_id=inflate.findViewById(R.id.tv_id);
            tv_id.setText(student.getMy_id()+"");//要将整数转成字符串

            TextView tv_name=inflate.findViewById(R.id.tv_name);
            tv_name.setText(student.getMy_name());

            TextView tv_time=inflate.findViewById(R.id.tv_date);
            tv_time.setText(student.getMy_date());

            return inflate;//返回创建定义参数
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.myop,menu);
//        menu.add(0,1,1,"关于");
//        menu.add(0,2,2,"账号");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();

        return true;
    }
}
